import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.MouseEvent;
import java.util.Vector;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import java.io.*;
import java.net.*;

public class ChatRoom extends MyEvent {

	private JFrame jChatRoom;
	private Vector<String> vtlstOnline;
	private JList<String> jlstOnline;
	private JButton jbtnToggle, jbtnExit, jbtnSend;
	private JTextField jtxtUsername, jtxtPassword, jtxtPort;
	private JLabel jlblUsername, jlblPassword, jlblPort;
	private JTextArea jtxtChatContent, jtxtMessage;
	private JScrollPane jscrollListOnline, jscrollChatContent, jscrollMessage;
	private JPanel jPanel;
	private Socket _serverSck = null;
	private BufferedReader _br = null;
	private PrintWriter _pw = null;

	public ChatRoom() {

		vtlstOnline = new Vector<>();
		jlstOnline = new JList<String>(vtlstOnline);
		jlstOnline.addMouseListener(this);

		jscrollListOnline = new JScrollPane(jlstOnline);
		jscrollListOnline.setPreferredSize(new Dimension(120, 321));
		jlstOnline.setPreferredSize(new Dimension(100, 300));

		jChatRoom = new JFrame("Chat room");
		jPanel = new JPanel();

		jbtnToggle = new JButton("Sign In");
		jbtnToggle.addActionListener(this);
		jbtnExit = new JButton("Exit");
		jbtnExit.addActionListener(this);
		jbtnSend = new JButton("Send");
		jbtnSend.setPreferredSize(new Dimension(120, 65));
		jbtnSend.setEnabled(false);
		jbtnSend.addActionListener(this);

		jtxtUsername = new JTextField(10);
		jtxtPassword = new JTextField(10);
		jtxtPort = new JTextField(9);

		jtxtChatContent = new JTextArea(20, 44);
		jtxtChatContent.setLineWrap(true);
		jtxtChatContent.setEnabled(false);
		jscrollChatContent = new JScrollPane(jtxtChatContent);

		jtxtMessage = new JTextArea(4, 44);
		jtxtMessage.setLineWrap(true);
		jscrollMessage = new JScrollPane(jtxtMessage);

		jlblUsername = new JLabel("Username");
		jlblPassword = new JLabel("Password");
		jlblPort = new JLabel("Port");

	}

	public void CreateGUI() {
		jPanel.add(jlblPort);
		jPanel.add(jtxtPort);
		jPanel.add(jlblUsername);
		jPanel.add(jtxtUsername);
		jPanel.add(jlblPassword);
		jPanel.add(jtxtPassword);
		jPanel.add(jbtnToggle);
		jPanel.add(jbtnExit);
		jPanel.add(jscrollChatContent);
		jPanel.add(jscrollListOnline);
		jPanel.add(jscrollMessage);
		jPanel.add(jbtnSend);

		jChatRoom.add(jPanel);
		jChatRoom.setSize(660, 480);
		jChatRoom.setResizable(false);
		jChatRoom.setVisible(true);
	}

	public static void main(String[] args) {
		ChatRoom gui = new ChatRoom();
		gui.CreateGUI();
	}

	@Override
	public void actionPerformed(ActionEvent e) {

		if (e.getSource() == jbtnToggle) {
			try {

				switch (e.getActionCommand()) {
				case "Sign In":

					jbtnToggle.setText("Sign Out");
					vtlstOnline.addElement(jtxtUsername.getText());
					jlstOnline.setListData(vtlstOnline);
					jtxtUsername.setEnabled(false);
					jtxtPassword.setEnabled(false);
					jtxtPort.setEnabled(false);
					jbtnSend.setEnabled(true);
					_serverSck = new Socket("localhost", 2000);
					break;

				case "Sign Out":
					jbtnToggle.setText("Sign In");
					vtlstOnline.remove(jtxtUsername.getText());
					jlstOnline.setListData(vtlstOnline);
					jtxtUsername.setEnabled(true);
					jtxtPassword.setEnabled(true);
					jtxtPort.setEnabled(true);
					jbtnSend.setEnabled(false);
					break;
				}
			} catch (Exception exception) {
				jtxtChatContent.setText(exception.getMessage());
			}

		} else if (e.getSource() == jbtnExit) {
			jChatRoom.setVisible(false);

		} else if (e.getSource() == jbtnSend) {
			if (jtxtMessage.getText().equals(""))
				return;
			String chatContent = jtxtChatContent.getText();
			String message = jtxtMessage.getText();
			//jtxtChatContent
				//	.setText(jtxtUsername.getText() + ": " + chatContent == "" ? message
					//		: chatContent + "\n" + message);
			try {
				_br = new BufferedReader(new InputStreamReader(_serverSck.getInputStream()));
				_pw = new PrintWriter(_serverSck.getOutputStream(), true);
				
					_pw.write(jtxtUsername.getText() + ": " + chatContent == "" ? message
							: chatContent + "\n" + message);
					jtxtChatContent.setText(_br.readLine());
				
			} catch (Exception e2) {
				// TODO: handle exception
			}
			
		}
	}

	@Override
	public void mouseClicked(MouseEvent e) {
		String selectValue = jlstOnline.getSelectedValue();
		if (!vtlstOnline.isEmpty() && !selectValue.equals("")) {
			ChatPrivate chatPrivate = new ChatPrivate(selectValue);
			chatPrivate.CreateGUI();
		}
	}
	
}
